package com.example.a7822.myapplication.shopcart.model;


import com.example.a7822.myapplication.shopcart.beans.ShopBean;
import com.example.a7822.myapplication.shopcart.common.Urls;

import java.util.List;

/**
 * Created by Flyln on 2016/6/14.
 */
public class ShopModelImpl implements ShopModel {
    private String tag = "GoodModelImpl";

    //加载购物车
    @Override
    public void loadShopList(String url, final int type , final OnLoadShopListListener listener) {
        //采用回调的方式异步通知收到的数据
    /*    OkHttpUtils.get(url, new OkHttpUtils.ResultCallback<String>() {
            @Override
            public void onSuccess(String response) {
                Log.d("ShopJSON", "请求店铺信息成功"+response);
                try {
                    JSONObject res = new JSONObject(response);
                    JSONObject shopinfo = res.getJSONObject("shopInfo");
                    JSONArray jsonArrayPhptos = shopinfo.getJSONArray("shopPicture");
                    String picUrl = jsonArrayPhptos.getString(0);
                    ShopBean shop = new ShopBean();

                    shop.setFavorite(false);//TODO 关注店铺缺少接口
                    shop.setImageUrl(shopinfo.getString("shopLogo"));
                    shop.setShopName(shopinfo.getString("shopName"));
                    shop.setShopDetail("");
                    shop.setShopPicUrl(picUrl);
                    shop.setShopRate(shopinfo.getInt("shopGrade") * 2);
                    shop.setShopBrief(shopinfo.getString("ShopBrief"));
                    shop.setShopId(shopinfo.getInt("id"));
                    shop.setShopDistance("6");//TODO 缺少店铺距离
                    shop.setType(0);
                    PinYin.getPinYin(shop.getShopName(),shop.getPinyinElement());
//                            ShopManager.getInstance().getShopModelList().add(shop);
//                            EventBus.getDefault().postSticky(ShopEvent.MAIN_DATA_OK);

                    listener.onLoadShopListSuccess(shop);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Exception e) {
//                        EventBus.getDefault().postSticky(ShopEvent.LOAD_DATA_FAILED);
                Log.e("ShopJSON","网络错误");
            }
        });
            listener.onLoadShopListSuccess(SPCartManager.getInstance().getGoodsMap());
        } else{
            listener.onLoadShopListFailure("load Cart list failure.",new Exception());
        }
        */
    }


    //加载一件商品
    @Override
    public void loadShopDetail(String url, List params, final OnLoadShopDetailListener listener) {
//        OkHttpUtils.ResultCallback<String> callback = new OkHttpUtils.ResultCallback<String>() {
//            @Override
//            public void onSuccess(String response) {
//                ShopBean shop = ShopJsonUtils.readJSONShopBean(response);
//                listener.onLoadShopDetailSuccess(shop);
//            }
//
//            @Override
//            public void onFailure(Exception e) {
//                listener.onLoadShopDetailFailure("load news list failure.", e);
//            }
//        };
//        OkHttpUtils.get(url, callback, params);
    }

    /**
     * 获取ID
     *
     * @param type
     * @return
     */
    private String getID(int type) {
        String id;
        switch (type) {
            case 0:
                id = Urls.TOP_ID;
                break;
            default:
                id = Urls.TOP_ID;
                break;
        }
        return id;
    }


    //监听购物车加载
    public interface OnLoadShopListListener {
        void onLoadShopListSuccess(List<ShopBean> list);
        void onLoadShopListFailure(String msg, Exception e);
    }
    //监听商品加载
    public interface OnLoadShopDetailListener {
        void onLoadShopDetailSuccess(ShopBean shopBean);
        void onLoadShopDetailFailure(String msg, Exception e);
    }
}
